home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Can anyone HELP with variable delaration? Please?
- Date: 3 Apr 1996 18:29:40 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4jvc6kINNk1j@keats.ugrad.cs.ubc.ca>
- References: <4jt07b$dul@news.us.net> <3162119a.43634755@news.ucs.ubc.ca> <4juf2kINNe9v@keats.ugrad.cs.ubc.ca> <3162de71.18703379@news.ucs.ubc.ca>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <3162de71.18703379@news.ucs.ubc.ca>,
- James Fairweather <jamesdf@unixg.ubc.ca> wrote:
- >>Any variable can be declared constant, including strings:
- >>
- >> const char * const str = "Foo";
- >>
- >>Or do you mean literal constants?
- >>
- >I'm not sure what the difference is. I thought the keyword "const"
- >was just for the compiler. If you tried to modify the variable
- >declared const, the compiler would complain. Does it do something
- >else with the variable if you declare it const?
-
- It's a language feature which designates an unmodifiable lvalue, as you say.
- Though around here we are careful to distinguish the C language itself from C
- language compilers. If you don't make that distinction, then a _lot_ of things
- easily become ``for the compiler only''. Structs are surely for the compiler
- only---after all, there are no data structures at the machine code level, just
- addressing!
-
- >I guess I was being too extreme. It's just that in high school, when
- >we were coding with Pascal, we were encouraged to use strings
- >whereever possible. Don't ask me why; it never occurred to me to ask
- >at the time. And sometimes when I read other people's code,
- >espeically people who are used to Pascal, I often see them using
- >strings in an if...else situation. In these cases, a well-name
- >#defined constant works better, because you can use switch and the
- >comparison is faster.
-
- Ah, I thought that's what you might have been referring to. This sort of abuse
- isn't something you would see too much of in C. In Pascal, which has string
- operators, it is more natural.
-
- A Pascal compiler can optimize some string comparisons into fast inline code.
- E,g, a comparison like IF string = "ABCD" might be turned into a straight
- 32-bit comparison on a suitable architecture.
-
- Nevertheless, whoever taught you that in highschool was plainly nuts. But then
- again, kooky ``computer science'' teachers are plentiful at the higschool
- level.
- --
-
-